home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_gimp-print.idb / usr / freeware / info / gimpprint.info-3.z / gimpprint.info-3
Text File  |  2002-10-07  |  46KB  |  1,008 lines

  1. This is gimpprint.info, produced by makeinfo version 4.0 from
  2. gimpprint.texi.
  3.  
  4. INFO-DIR-SECTION Libraries
  5. START-INFO-DIR-ENTRY
  6. * GIMP-Print: (gimpprint).      print plugin for the GIMP, and printing library
  7. END-INFO-DIR-ENTRY
  8.  
  9.    This file documents the gimpprint library and associated programs
  10. used for high quality printing.
  11.  
  12.    Copyright (C) 2001 Michael Sweet (<mike@easysw.com>) and Robert
  13. Krawitz (<rlk@alum.mit.edu>)
  14.  
  15.    Permission is granted to make and distribute verbatim copies of this
  16. manual provided the copyright notice and this permission notice are
  17. preserved on all copies.
  18.  
  19.    Permission is granted to copy and distribute modified versions of
  20. this manual under the conditions for verbatim copying, provided that
  21. the entire resulting derived work is distributed under the terms of a
  22. permission notice identical to this one.
  23.  
  24.    Permission is granted to copy and distribute translations of this
  25. manual into another language, under the above conditions for modified
  26. versions, except that this permission notice may be stated in a
  27. translation approved by the Foundation.
  28.  
  29. 
  30. File: gimpprint.info,  Node: Weaving introduction,  Next: Weaving algorithms,  Up: Weaving
  31.  
  32. Introduction
  33. ============
  34.  
  35.    The Epson Stylus Color/Photo printers don't have memory to print
  36. using all of the nozzles in the print head.  For example, the Stylus
  37. Photo 700/EX has 32 nozzles.  At 720 dpi, with an 8" wide image, a
  38. single line requires (8 * 720 * 6 / 8) bytes, or 4320 bytes (because the
  39. Stylus Photo printers have 6 ink colors).  To use 32 nozzles per color
  40. would require 138240 bytes.  It's actually worse than that, though,
  41. because the nozzles are spaced 8 rows apart.  Therefore, in order to
  42. store enough data to permit sending the page as a simple raster, the
  43. printer would require enough memory to store 256 rows, or 1105920 bytes.
  44. Considering that the Photo EX can print 11" wide, we're looking at more
  45. like 1.5 MB.  In fact, these printers are capable of 1440 dpi horizontal
  46. resolution.  This would require 3 MB.  The printers actually have
  47. 64K-256K.
  48.  
  49.    With the newer (740/750 and later) printers it's even worse, since
  50. these printers support multiple dot sizes; of course, the even newer
  51. 2880x720 printers don't help either.
  52.  
  53.    Older Epson printers had a mode called "MicroWeave" (tm).  In this
  54. mode, the host fed the printer individual rows of dots, and the printer
  55. bundled them up and sent them to the print head in the correct order to
  56. achieve high quality.  This MicroWeave mode still works in new printers,
  57. but in some cases the implementation is very minimal: the printer uses
  58. exactly one nozzle of each color (the first one).  This makes printing
  59. extremely slow (more than 30 minutes for one 8.5x11" page), although the
  60. quality is extremely high with no visible banding whatsoever.  It's not
  61. good for the print head, though, since no ink is flowing through the
  62. other nozzles.  This leads to drying of ink and possible permanent
  63. damage to the print head.
  64.  
  65.    By the way, although the Epson manual says that microweave mode
  66. should be used at 720 dpi, 360 dpi continues to work in much the same
  67. way.  At 360 dpi, data is fed to the printer one row at a time on all
  68. Epson printers.  The pattern that the printer uses to print is very
  69. prone to banding.  However, 360 dpi is inherently a low quality mode;
  70. if you're using it, presumably you don't much care about quality.  It
  71. is possible to do microweave at 360 DPI, with significantly improved
  72. quality.
  73.  
  74.    Except for the Stylus Pro printers (5000, 5500, 7000, 7500, 9000,
  75. 9500, and when it's released the 10000), which can do microweave at any
  76. resolution, printers from roughly the Stylus Color 600 and later do not
  77. have the capability to do MicroWeave correctly in many cases (some
  78. printers can do MicroWeave correctly at 720 DPI).  Instead, the host
  79. must arrange the output in the order that it will be sent to the print
  80. head.  This is a very complex process; the jets in the print head are
  81. spaced more than one row (1/720") apart, so we can't simply send
  82. consecutive rows of dots to the printer.  Instead, we have to pass e.
  83. g. the first, ninth, 17th, 25th... rows in order for them to print in
  84. the correct position on the paper.  This interleaving process is called
  85. "soft" weaving.
  86.  
  87.    This decision was probably made to save money on memory in the
  88. printer.  It certainly makes the driver code far more complicated than
  89. it would be if the printer could arrange the output.  Is that a bad
  90. thing?  Usually this takes far less CPU time than the dithering
  91. process, and it does allow us more control over the printing process,
  92. e.g. to reduce banding.  Conceivably, we could even use this ability to
  93. map out bad jets.
  94.  
  95.    Interestingly, apparently the Windows (and presumably Macintosh)
  96. drivers for most or all Epson printers still list a "microweave" mode.
  97. Experiments have demonstrated that this does not in fact use the
  98. "microweave" mode of the printer.  Possibly it does nothing, or it uses
  99. a different weave pattern from what the non-"microweave" mode does.
  100. This is unnecessarily confusing, at least for people who write drivers
  101. who try to explain them to people who don't.
  102.  
  103.    What makes this interesting is that there are many different ways of
  104. of accomplishing this goal.  The naive way would be to divide the image
  105. up into groups of 256 rows (for a printer with 32 jets and a separation
  106. of 8 rows), and print all the mod8=0 rows in the first pass, mod8=1
  107. rows in the second, and so forth.  The problem with this approach is
  108. that the individual ink jets are not perfectly uniform; some emit
  109. slightly bigger or smaller drops than others.  Since each group of 8
  110. adjacent rows is printed with the same nozzle, that means that there
  111. will be distinct streaks of lighter and darker bands within the image
  112. (8 rows is 1/90", which is visible; 1/720" is not).  Possibly worse is
  113. that these patterns will repeat every 256 rows.  This creates banding
  114. patterns that are about 1/3" wide.
  115.  
  116.    So we have to do something to break up this patterning.
  117.  
  118.    Epson does not publish the weaving algorithms that they use in their
  119. bundled drivers.  Indeed, their developer web site
  120. (http://www.ercipd.com/isv/edr_docs.htm) does not even describe how to
  121. do this weaving at all; it says that the only way to achieve 720 dpi is
  122. to use MicroWeave.  It does note (correctly) that 1440 dpi horizontal
  123. can only be achieved by the driver (i. e. in software).  The manual
  124. actually makes it fairly clear how to do this (it requires two passes
  125. with horizontal head movement between passes), and it is presumably
  126. possible to do this with MicroWeave.
  127.  
  128.    The information about how to do this is apparently available under
  129. non-disclosure agreement (NDA).  It's actually easy enough to reverse
  130. engineer what's inside a print file with a simple Perl script, which is
  131. supplied with the Gimp-Print distribution as tests/parse-escp2.  In any
  132. event, we weren't particularly interested in the weaving patterns Epson
  133. used.  There are many factors that go into choosing a good weaving
  134. pattern; we're learning them as we go along.  Issues such as drying time
  135. (giving the ink a few seconds more or less to dry can have highly
  136. visible effects) affect the quality of the output.
  137.  
  138.    The Uniprint GhostScript driver has been able to do weaving for a
  139. long time.  It uses patterns that must be specified for each choice of
  140. resolution and printer.  We preferred an algorithmic approach that
  141. computes a weave pattern for any given choice of inputs.  This
  142. obviously requires extensive testing; we developed a test suite
  143. specifically for this purpose.
  144.  
  145. 
  146. File: gimpprint.info,  Node: Weaving algorithms,  Prev: Weaving introduction,  Up: Weaving
  147.  
  148. Weaving algorithms
  149. ==================
  150.  
  151.    I considered a few algorithms to perform the weave.  The first one I
  152. devised let me use only (jets-distance_between_jets+1) nozzles, or 25.
  153. This is OK in principle, but it's slower than using all nozzles.  By
  154. playing around with it some more, I came up with an algorithm that lets
  155. me use all of the nozzles, except near the top and bottom of the page.
  156.  
  157.    This still produces some banding, though.  Even better quality can be
  158. achieved by using multiple nozzles on the same line.  How do we do
  159. this?  In 1440x720 mode, we're printing two output lines at the same
  160. vertical position.  However, if we want four passes, we have to
  161. effectively print each line twice.  Actually doing this would increase
  162. the density, so what we do is print half the dots on each pass.  This
  163. produces near-perfect output, and it's far faster than using (pseudo)
  164. "MicroWeave".
  165.  
  166.    Yet another complication is how to get near the top and bottom of the
  167. page.  This algorithm lets us print to within one head width of the top
  168. of the page, and a bit more than one head width from the bottom.  That
  169. leaves a lot of blank space.  Doing the weave properly outside of this
  170. region is increasingly difficult as we get closer to the edge of the
  171. paper; in the interior region, any nozzle can print any line, but near
  172. the top and bottom edges, only some nozzles can print.  We originally
  173. handled this by using the naive way mentioned above near the borders,
  174. and switching over to the high quality method in the interior.
  175. Unfortunately, this meant that the quality is quite visibly degraded
  176. near the top and bottom of the page.  We have since devised better
  177. algorithms that allow printing to the extreme top and bottom of the
  178. region that can physically be printed, with only minimal loss of
  179. quality.
  180.  
  181.    Epson does not advertise that the printers can print at the very top
  182. of the page, although in practice most of them can.  The quality is
  183. degraded to some degree, and we have observed that in some cases not
  184. all of the dots get printed.  Epson may have decided that the
  185. degradation in quality is sufficient that printing in that region
  186. should not be allowed.  That is a valid decision, although we have
  187. taken another approach.
  188.  
  189. * Menu:
  190.  
  191. * Simple weaving algorithms::   Starting to weave.
  192. * Perfect weaving::             Improving the weave.
  193. * Weaving collisions::          Bang!
  194. * What is perfect weaving?::    What makes a ``perfect'' weave?
  195. * Oversampling::                Increasing resolution, reducing banding
  196.  
  197. 
  198. File: gimpprint.info,  Node: Simple weaving algorithms,  Next: Perfect weaving,  Prev: Weaving algorithms,  Up: Weaving algorithms
  199.  
  200. Simple weaving algorithms
  201. -------------------------
  202.  
  203.    The initial problem is to calculate the starting position of each
  204. pass; the row number of the printer's top jet when printing that pass.
  205. Since we assume the paper cannot be reverse-fed, the print head must,
  206. for each pass, start either further down the page than the previous
  207. pass or at the same position.  Each pass's start point is therefore at
  208. a non-negative offset from the previous pass's start point.
  209.  
  210.    Once we have a formula for the starting row of each pass, we then
  211. turn that "inside out" to get a formula for the pass number containing
  212. each row.
  213.  
  214.    First, let's define how our printer works.  We measure vertical
  215. position on the paper in "rows"; the resolution with which the printer
  216. can position the paper vertically.  The print head contains J ink jets,
  217. which are spaced S rows apart.
  218.  
  219.    Consider a very simple case: we want to print a page as quickly as
  220. possible, and we mostly don't care how sparse the printing is, so long
  221. as it's fairly even.
  222.  
  223.    It's pretty obvious how to do this.  We make one pass with the print
  224. head, printing J lines of data, each line S rows after the previous
  225. one.  We then advance the paper by S*J rows and print the next row.
  226. For example, if J=7 and S=4, this method can be illustrated like this:
  227.  
  228.      pass number
  229.      | row number------->
  230.      | |         111111111122222222223333333333444444444455555555556666666666
  231.      | 0123456789012345678901234567890123456789012345678901234567890123456789
  232.      0 *---*---*---*---*---*---*
  233.      1                             *---*---*---*---*---*---*
  234.      2 \-----------------------/                               *---*---*---*---*---*-
  235.                7 jets              \---/
  236.                                    4 rows offset from one jet to the next
  237.        \---------------------------/
  238.           7*4=28 rows offset from one pass to the next
  239.  
  240.    In these examples, the vertical axis can be thought of as the time
  241. axis, with the pass number shown at the left margin, while the row
  242. number runs horizontally.  A `*' shows each row printed by a pass, and
  243. a row of `-' is used to link together the rows printed by one pass of
  244. the print head.  The first pass is numbered `0' and starts at row 0.
  245. Each subsequent pass p starts at row p*S*J.  Each pass prints J lines,
  246. each line being S rows after the previous one.  (For ease of viewing
  247. this file on a standard terminal, I'm clipping the examples at column
  248. 80.)
  249.  
  250.    This method covers the whole page with lines printed evenly S rows
  251. apart.  However, we want to fill in all the other rows with printing to
  252. get a full-density page (we're ignoring oversampling at this stage).
  253. Where we have previously printed a single pass, we'll now print a "pass
  254. block": we print extra passes to fill in the empty rows.  A naive
  255. implementation might look like this:
  256.  
  257.      0 *---*---*---*---*---*---*
  258.      1  *---*---*---*---*---*---*
  259.      2   *---*---*---*---*---*---*
  260.      3    *---*---*---*---*---*---*
  261.      4                             *---*---*---*---*---*---*
  262.      5                              *---*---*---*---*---*---*
  263.      6                               *---*---*---*---*---*---*
  264.      7                                *---*---*---*---*---*---*
  265.      8                                                         *---*---*---*---*---*-
  266.      9                                                          *---*---*---*---*---*
  267.      10                                                          *---*---*---*---*---
  268.      11                                                           *---*---*---*---*--
  269.  
  270. (Now you can see why this process is called "weaving"!)
  271.  
  272. 
  273. File: gimpprint.info,  Node: Perfect weaving,  Next: Weaving collisions,  Prev: Simple weaving algorithms,  Up: Weaving algorithms
  274.  
  275. Perfect weaving
  276. ---------------
  277.  
  278.    This simple weave pattern prints every row, but will give conspicuous
  279. banding patterns for the reasons discussed above.
  280.  
  281.    Let's start improving this for our simple case.  We can reduce
  282. banding by making sure that any given jet never prints a row too close
  283. to another row printed by the same jet.  This means we want to space the
  284. rows printed by a given jet evenly down the page.  In turn, this
  285. implies we want to advance the paper by as nearly an equal amount after
  286. each pass as possible.
  287.  
  288.    Each pass block prints S*J lines in S passes.  The first line
  289. printed in each pass block is S*J rows lower on the page than the first
  290. line printed in the previous pass block.  Therefore, if we advance the
  291. paper by J rows between each pass, we can print the right number of
  292. passes in each block and advance the paper perfectly evenly.
  293.  
  294.    Here's what this "perfect" weave looks like:
  295.  
  296.                          start of full weave
  297.                          |
  298.      0 *---*---*---*---*---*---*
  299.      1        *---*---*---*---*---*---*
  300.      2               *---*---*---*---*---*---*
  301.      3                      *---*---*---*---*---*---*
  302.      4                             *---*---*---*---*---*---*
  303.      5                                    *---*---*---*---*---*---*
  304.      6                                           *---*---*---*---*---*---*
  305.      7                                                  *---*---*---*---*---*---*
  306.      8                                                         *---*---*---*---*---*-
  307.      9                                                                *---*---*---*--
  308.      10                                                                      *---*---
  309.      11                                                                             *
  310.  
  311.    You'll notice that, for the first few rows, this weave is too sparse.
  312. It is not until the row marked "start of full weave" that every
  313. subsequent row is printed.  We can calculate this start position as
  314. follows:
  315.  
  316.      start = (S-1) * (J-1)
  317.  
  318.    For the moment, we will ignore this problem with the weave.  We'll
  319. consider later how to fill in the missing rows.
  320.  
  321.    Let's look at a few more examples of perfect weaves:
  322.  
  323. S=2,  J=7,  start=(2-1)*(7-1)=6:
  324.  
  325.              starting row of full weave
  326.              |
  327.      0 *-*-*-*-*-*-*
  328.      1        *-*-*-*-*-*-*
  329.      2               *-*-*-*-*-*-*
  330.      3                      *-*-*-*-*-*-*
  331.      4                             *-*-*-*-*-*-*
  332.      5                                    *-*-*-*-*-*-*
  333.      6                                           *-*-*-*-*-*-*
  334.      7                                                  *-*-*-*-*-*-*
  335.  
  336. S=7,  J=2,  start=6:
  337.  
  338.              start
  339.              |
  340.      0 *------*
  341.      1   *------*
  342.      2     *------*
  343.      3       *------*
  344.      4         *------*
  345.      5           *------*
  346.      6             *------*
  347.      7               *------*
  348.      8                 *------*
  349.      9                   *------*
  350.  
  351. S=4,  J=13,  start=36:
  352.  
  353.                                            start
  354.                                            |
  355.      0 *---*---*---*---*---*---*---*---*---*---*---*---*
  356.      1              *---*---*---*---*---*---*---*---*---*---*---*---*
  357.      2                           *---*---*---*---*---*---*---*---*---*---*---*---*
  358.      3                                        *---*---*---*---*---*---*---*---*---*--
  359.      4                                                     *---*---*---*---*---*---*-
  360.      5                                                                  *---*---*---*
  361.  
  362. S=13,  J=4,  start=36:
  363.  
  364.                                            start
  365.                                            |
  366.      0 *------------*------------*------------*
  367.      1     *------------*------------*------------*
  368.      2         *------------*------------*------------*
  369.      3             *------------*------------*------------*
  370.      4                 *------------*------------*------------*
  371.      5                     *------------*------------*------------*
  372.      6                         *------------*------------*------------*
  373.      7                             *------------*------------*------------*
  374.      8                                 *------------*------------*------------*
  375.      9                                     *------------*------------*------------*
  376.      10                                        *------------*------------*-----------
  377.      11                                            *------------*------------*-------
  378.      12                                                *------------*------------*---
  379.      13                                                    *------------*------------
  380.      14                                                        *------------*--------
  381.      15                                                            *------------*----
  382.      16                                                                *------------*
  383.      17                                                                    *---------
  384.      18                                                                        *-----
  385.      19                                                                            *-
  386.  
  387. S=8,  J=5,  start=28:
  388.  
  389.                                    start
  390.                                    |
  391.      0 *-------*-------*-------*-------*
  392.      1      *-------*-------*-------*-------*
  393.      2           *-------*-------*-------*-------*
  394.      3                *-------*-------*-------*-------*
  395.      4                     *-------*-------*-------*-------*
  396.      5                          *-------*-------*-------*-------*
  397.      6                               *-------*-------*-------*-------*
  398.      7                                    *-------*-------*-------*-------*
  399.      8                                         *-------*-------*-------*-------*
  400.      9                                              *-------*-------*-------*-------*
  401.      10                                                  *-------*-------*-------*---
  402.      11                                                       *-------*-------*------
  403.      12                                                            *-------*-------*-
  404.      13                                                                 *-------*----
  405.      14                                                                      *-------
  406.      15                                                                           *--
  407.  
  408. S=9,  J=5,  start=32:
  409.  
  410.                                        start
  411.                                        |
  412.      0 *--------*--------*--------*--------*
  413.      1      *--------*--------*--------*--------*
  414.      2           *--------*--------*--------*--------*
  415.      3                *--------*--------*--------*--------*
  416.      4                     *--------*--------*--------*--------*
  417.      5                          *--------*--------*--------*--------*
  418.      6                               *--------*--------*--------*--------*
  419.      7                                    *--------*--------*--------*--------*
  420.      8                                         *--------*--------*--------*--------*
  421.      9                                              *--------*--------*--------*-----
  422.      10                                                  *--------*--------*--------*
  423.      11                                                       *--------*--------*----
  424.      12                                                            *--------*--------
  425.      13                                                                 *--------*---
  426.      14                                                                      *-------
  427.      15                                                                           *--
  428.  
  429. S=6,  J=7,  start=30:
  430.  
  431.                                      start
  432.                                      |
  433.      0 *-----*-----*-----*-----*-----*-----*
  434.      1        *-----*-----*-----*-----*-----*-----*
  435.      2               *-----*-----*-----*-----*-----*-----*
  436.      3                      *-----*-----*-----*-----*-----*-----*
  437.      4                             *-----*-----*-----*-----*-----*-----*
  438.      5                                    *-----*-----*-----*-----*-----*-----*
  439.      6                                           *-----*-----*-----*-----*-----*-----
  440.      7                                                  *-----*-----*-----*-----*----
  441.      8                                                         *-----*-----*-----*---
  442.      9                                                                *-----*-----*--
  443.      10                                                                      *-----*-
  444.      11                                                                             *
  445.  
  446. 
  447. File: gimpprint.info,  Node: Weaving collisions,  Next: What is perfect weaving?,  Prev: Perfect weaving,  Up: Weaving algorithms
  448.  
  449. Weaving collisions
  450. ------------------
  451.  
  452.    This perfect weave is not possible in all cases.  Let's look at
  453. another example:
  454.  
  455. S=6,  J=4:
  456.  
  457.      0 *-----*-----*-----*
  458.      1     *-----*-----*-----*
  459.      2         *-----*-----*-----*
  460.      3             *-----*-----*-----*
  461.      4             ^   *-^---*-----*-----*
  462.      5             |   ^ | *-^---*-----*-----*
  463.                    OUCH!   ^ |   ^
  464.                            |     |
  465.  
  466. Here we have a collision.  Some lines printed in later passes overprint
  467. lines printed by earlier passes.  We can see why by considering which
  468. row number is printed by a given jet number j (numbered from 0) of a
  469. given pass, p:
  470.  
  471.      row(p, j) = p*J + j*S
  472.  
  473.    Because J=4 and S=6 have a common factor of 2, jet 2 of pass 0
  474. prints the same row as jet 0 of pass 3:
  475.  
  476.      row(0, 2) = 0*4 + 2*6 = 12
  477.      row(3, 0) = 3*4 + 0*6 = 12
  478.  
  479.    In fact, with this particular weave pattern, jets 0 and 1 of pass
  480. p+3 always overprint jets 2 and 3 of pass p.  We'll represent
  481. overprinting rows by a `^' in our diagrams, and correct rows by `*':
  482.  
  483. S=6  J=4:
  484.  
  485.      0 *-----*-----*-----*
  486.      1     *-----*-----*-----*
  487.      2         *-----*-----*-----*
  488.      3             ^-----^-----*-----*
  489.      4                 ^-----^-----*-----*
  490.      5                     ^-----^-----*-----*
  491.  
  492. 
  493. File: gimpprint.info,  Node: What is perfect weaving?,  Next: Oversampling,  Prev: Weaving collisions,  Up: Weaving algorithms
  494.  
  495. What makes a "perfect" weave?
  496. -----------------------------
  497.  
  498.    So what causes the perfect weave cases to be perfect, and the other
  499. cases not to be?  In all the perfect cases above, S and J are
  500. relatively prime (i.e. their greatest common divisor (GCD) is 1).  As
  501. we mentioned above, S=6 and J=4 have a common factor, which causes the
  502. overprinting.  Where S and J have a GCD of 1, they have no common
  503. factor other than 1 and, as a result, no overprinting occurs.  If S and
  504. J are not relatively prime, their common factor will cause overprinting.
  505.  
  506.    We can work out the greatest common divisor of a pair of natural
  507. numbers using Euler's algorithm:
  508.  
  509.    * Start with the two numbers:                        (e.g.)  9,  24
  510.  
  511.    * Swap them if necessary so that the larger one comes first: 24,   9
  512.  
  513.    * Subtract the second number from the first:                 15,   9
  514.  
  515.    * Repeat until the first number becomes smaller:              6,   9
  516.  
  517.    * Swap the numbers again, so the larger one comes first:      9,   6
  518.  
  519.    * Subtract again:                                             3,   6
  520.  
  521.    * Swap:                                                       6,   3
  522.  
  523.    * Subtract:                                                   3,   3
  524.  
  525.    * And again:                                                  0,   3
  526.  
  527.    * When one of the numbers becomes 0, the other number is the GCD of
  528.      the two numbers you started with.
  529.  
  530.    These repeated subtractions can be done with C's `%' operator, so we
  531. can write this in C as follows:
  532.  
  533.      unsigned int
  534.      gcd(unsigned int x, unsigned int y)
  535.      {
  536.          if (y == 0)
  537.              return x;
  538.          while (x != 0) {
  539.              if (y > x)
  540.                  swap (&x, &y);
  541.              x %= y;
  542.          }
  543.          return y;
  544.      }
  545.  
  546.    `gcd(S,J)' will feature quite prominently in our weaving algorithm.
  547.  
  548.    If 0 <= j < J, there should only be a single pair (p, j) for any
  549. given row number.  If S and J are not relatively prime, this assumption
  550. breaks down.  (For conciseness, let G=GCD(S,J).)
  551.  
  552. S=8,  J=6,  G=2:
  553.  
  554.      0 *-------*-------*-------*-------*-------*
  555.      1       *-------*-------*-------*-------*-------*
  556.      2             *-------*-------*-------*-------*-------*
  557.      3                   *-------*-------*-------*-------*-------*
  558.      4                         ^-------^-------^-------*-------*-------*
  559.      5                               ^-------^-------^-------*-------*-------*
  560.  
  561.    In this case, jets 0, 1 and 2 of pass p+4 collide with jets 3, 4 and
  562. 5 of pass p.
  563.  
  564.    How can we calculate these numbers?  Suppose we were to print using
  565. fewer jets, say J/G jets.  The greatest common divisor of J/G and S is
  566. 1, enabling a perfect weave.  But to get a perfect weave, we also have
  567. to advance the paper by a factor of G less:
  568.  
  569.      0 *-------*-------*       -       -       -
  570.      1    *-------*-------*       -       -       -
  571.      2       *-------*-------*       -       -       -
  572.      3          *-------*-------*       -       -       -
  573.      4             *-------*-------*       -       -       -
  574.      5                *-------*-------*       -       -       -
  575.  
  576.    If we left the paper advance alone, we'd get a sparse weave; only one
  577. row can be printed every G rows:
  578.  
  579.      0 *-------*-------*       -       -       -
  580.      1       *-------*-------*       -       -       -
  581.      2             *-------*-------*       -       -       -
  582.      3                   *-------*-------*       -       -       -
  583.      4                         *-------*-------*       -       -       -
  584.      5                               *-------*-------*       -       -       -
  585.                     ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
  586.                    These rows need filling in.
  587.  
  588.    The rows that would have been printed by the jets we've now omitted
  589. (shown as `-') are printed by other jets on later passes.
  590.  
  591.    Let's analyse this.  Consider how a pass p could collide with pass
  592. 0.  Pass p starts at offset p*J.  Pass 0 prints at rows which are
  593. multiples of S.  If p*J is exactly divisible by S, a collision has
  594. occurred, unless p*J >= J*S (which will happen when we finish a pass
  595. block).
  596.  
  597.    So, we want to find p and q such that p*J=q*S and p is minimised.
  598. Then p is the number of rows before a collision, and q is the number of
  599. jets in pass 0 which are not involved in the collision.  To do this, we
  600. find the lowest common multiple of J and S, which is L=J*S/G.  L/J is
  601. the number of rows before a collision, and L/S is the number of jets in
  602. the first pass not involved in the collision.
  603.  
  604.    Thus, we see that the first J/G rows printed by a given pass are not
  605. overprinted by any later pass.  However, the rest of the rows printed
  606. by pass p are overprinted by the first J-(J/G) jets of pass p+(S/G).
  607. We will use C to refer to S/G, the number of rows after which a
  608. collision occurs.
  609.  
  610.    Another example:
  611.  
  612. S=6,  J=9,  G=3,  C=S/G=2:
  613.  
  614.      0 *-----*-----*-----*-----*-----*-----*-----*-----*
  615.      1          *-----*-----*-----*-----*-----*-----*-----*-----*
  616.      2                   ^-----^-----^-----^-----^-----^-----*-----*-----*
  617.      3                            ^-----^-----^-----^-----^-----^-----*-----*-----*
  618.      4                                     ^-----^-----^-----^-----^-----^-----*-----
  619.      5                                              ^-----^-----^-----^-----^-----^--
  620.               ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^
  621.                    These rows need filling in.
  622.  
  623. In this case, the first J-(J/G) = 9-9/3 = 6 jets of pass p+(6/3)=p+2
  624. collide with the last 6 jets of pass p.  Only one row in every G=2 rows
  625. is printed by this weave.
  626.  
  627. S=9,  J=6,  G=3,  C=3:
  628.  
  629.      0 *--------*--------*--------*--------*--------*
  630.      1       *--------*--------*--------*--------*--------*
  631.      2             *--------*--------*--------*--------*--------*
  632.      3                   ^--------^--------^--------^--------*--------*
  633.      4                         ^--------^--------^--------^--------*--------*
  634.      5                               ^--------^--------^--------^--------*--------*
  635.  
  636. Here, the first J-(J/G) = 6-6/3 = 4 jets of pass p+(9/3)=p+3 collide
  637. with the last 4 jets of pass p.
  638.  
  639.    Note that, in these overprinting cases, only rows divisible by G are
  640. ever printed.  The other rows, those not divisible by G, are not
  641. touched by this weave.
  642.  
  643.    We can modify our weave pattern to avoid overprinting any rows and
  644. simultaneously fill in the missing rows.  Instead of using J alone to
  645. determine the start of each pass from the previous pass, we adjust the
  646. starting position of some passes.  As mentioned before, we will divide
  647. the page into pass blocks, with S passes in each block.  This ensures
  648. that the first jet of the first pass in a block prints the row which
  649. the Jth jet of the first pass of the previous block would have printed,
  650. if the print head had one extra jet.
  651.  
  652.    Looking back at an example of a perfect weave, we can divide it into
  653. pass blocks:
  654.  
  655. S=7,  J=2,  G=1:
  656.  
  657.                      imaginary extra jet
  658.                      |
  659.      0 *------*      *      <--start of pass block 0
  660.      1   *------*    |
  661.      2     *------*  |
  662.      3       *------*|
  663.      4         *-----|*
  664.      5           *---|--*
  665.      6             *-|----*
  666.                      |
  667.      7               *------*  <--start of pass block 1
  668.      8                 *------*
  669.      9                   *------*
  670.  
  671.    We can now calculate the start of a given pass by reference to its
  672. pass block.  The first pass of pass block b always starts at row
  673. (b*S*J).  The start row of each of the other passes in the block are
  674. calculated using offsets from this row.
  675.  
  676.    For the example above, there are 7 passes in each pass block, and
  677. their offsets are 0, 2, 4, 6, 8, 10 and 12.  The next pass block is
  678. offset S*J=14 rows from the start of the current pass block.
  679.  
  680.    The simplest way to modify the "perfect" weave pattern to give a
  681. correct weave in cases where G!=1 is to simply change any offsets which
  682. would result in a collision, until the collision disappears.  Every
  683. printed row in the weave, as we have shown it up to now, is separated
  684. from each of its neighbouring printed rows by G blank rows.  We will
  685. add an extra offset to each colliding pass in such a way that we push
  686. the pass onto these otherwise blank rows.
  687.  
  688.    We have seen that, unless G=1, the plain weave pattern results in
  689. each pass colliding with the pass S/G passes before.  We will now
  690. subdivide our pass block into subblocks, each consisting of B=S/G
  691. passes.  There are therefore G subblocks in a pass block.
  692.  
  693.    For each subblock, the passes in that subblock have a constant offset
  694. added to them.  The offset is different for each subblock in a block.
  695. There are many ways we can choose the offsets, but the simplest is to
  696. make the offset equal to the subblock number (starting from 0).
  697.  
  698.    Thus, the passes in the first subblock in each pass block remain at
  699. the offsets we've already calculated from J.  The passes in the second
  700. subblock each have 1 added to their offset, the passes in the third
  701. subblock have 2 added, and so on.  Thus, the offset of pass p (numbered
  702. relative to the start of its pass block) is p*J + floor(p/B).
  703.  
  704.    This gives us a weave pattern looking like this:
  705.  
  706. S=6,  J=9,  G=3,  B=2:
  707.  
  708.      0 *-----*-----*-----*-----*-----*-----*-----*-----*
  709.      1 ^        *-----*-----*-----*-----*-----*-----*-----*-----*
  710.      2 |              +-> *-----*-----*-----*-----*-----*-----*-----*-----*
  711.      3 |              |            *-----*-----*-----*-----*-----*-----*-----*-----*
  712.      4 |              |                  +-> *-----*-----*-----*-----*-----*-----*---
  713.      5 |              |                  |            *-----*-----*-----*-----*-----*
  714.      6 |              |                  |               +-> *-----*-----*-----*-----
  715.      7 |              |                  |               |            *-----*-----*--
  716.        |              |                  |             start of pass block 1
  717.        |              |                  |             (offset returns to 0)
  718.        |              |                  start of subblock 2 (offset 2 rows)
  719.        |              start of subblock 1 (following passes offset by 1 row)
  720.        start of passblock 0, subblock 0 (pass start calculated as p*J)
  721.  
  722. S=9,  J=6,  G=3,  B=3:
  723.  
  724.      0 *--------*--------*--------*--------*--------*
  725.      1       *--------*--------*--------*--------*--------*
  726.      2             *--------*--------*--------*--------*--------*
  727.      3                    *--------*--------*--------*--------*--------*
  728.      4                          *--------*--------*--------*--------*--------*
  729.      5                                *--------*--------*--------*--------*--------*
  730.      6                                       *--------*--------*--------*--------*---
  731.      7                                             *--------*--------*--------*------
  732.      8                                                   *--------*--------*--------*
  733.      9                                                       *--------*--------*-----
  734.      10                                                  \---/     *--------*--------
  735.      11                                               small offset       *--------*--
  736.      12                                                                         *----
  737.  
  738.    This method of choosing offsets for subblocks can result in an
  739. occasional small offset (as shown above) between one pass and the next,
  740. particularly when G is large compared to J.  For example:
  741.  
  742. S=8,  J=4,  G=4,  B=2:
  743.  
  744.      0 *-------*-------*-------*
  745.      1     *-------*-------*-------*
  746.      2          *-------*-------*-------*
  747.      3              *-------*-------*-------*
  748.      4                   *-------*-------*-------*
  749.      5                       *-------*-------*-------*
  750.      6                            *-------*-------*-------*
  751.      7                                *-------*-------*-------*
  752.      8                                 *-------*-------*-------*
  753.      9                                \/   *-------*-------*-------*
  754.                                    very small offset!
  755.  
  756.    We can plot the offset against the subblock number as follows:
  757.  
  758.      subblock number
  759.      | offset
  760.      | |
  761.      | 0123
  762.      0 *
  763.      1  *
  764.      2   *
  765.      3    *
  766.      0 *
  767.      1  *
  768.      2   *
  769.      3    *
  770.  
  771. The discontinuity in this plot results in the small offset between
  772. passes.
  773.  
  774.    As we said at the beginning, we want the offsets from each pass to
  775. the next to be as similar as possible.  We can fix this by calculating
  776. the offset for a given subblock b as follows:
  777.  
  778.        offset(b) = 2*b             , if b < ceiling(G/2)
  779.                  = 2*(G-b)-1       , otherwise
  780.  
  781.    We can visualise this as follows, for G=10:
  782.  
  783.        0123456789
  784.      0 *
  785.      1   *
  786.      2     *
  787.      3       *
  788.      4         *
  789.      5          *
  790.      6        *
  791.      7      *
  792.      8    *
  793.      9  *
  794.      0 *
  795.      1   *
  796.      2     *
  797.      3       *
  798.      4         *
  799.      5          *
  800.      6        *
  801.      7      *
  802.      8    *
  803.      9  *
  804.  
  805. and for G=11:
  806.  
  807.                   1
  808.         01234567890
  809.       0 *
  810.       1   *
  811.       2     *
  812.       3       *
  813.       4         *
  814.       5           *
  815.       6          *
  816.       7        *
  817.       8      *
  818.       9    *
  819.      10  *
  820.       0 *
  821.       1   *
  822.       2     *
  823.       3       *
  824.       4         *
  825.       5           *
  826.       6          *
  827.       7        *
  828.       8      *
  829.       9    *
  830.      10  *
  831.  
  832. This gives a weave looking like this:
  833.  
  834. S=12,  J=6,  G=6,  B=2:
  835.  
  836.      0 *-----------*-----------*-----------*-----------*-----------*
  837.      1       *-----------*-----------*-----------*-----------*-----------*
  838.      2               *-----------*-----------*-----------*-----------*-----------*
  839.      3                     *-----------*-----------*-----------*-----------*---------
  840.      4                             *-----------*-----------*-----------*-----------*-
  841.      5                                   *-----------*-----------*-----------*-------
  842.      6                                          *-----------*-----------*-----------*
  843.      7                                                *-----------*-----------*------
  844.      8                                                    *-----------*-----------*--
  845.      9                                                          *-----------*--------
  846.      10                                                             *-----------*----
  847.      11                                                                   *----------
  848.      12                                                                        *-----
  849.  
  850.    This method ensures that the offset between passes is always in the
  851. range [J-2,J+2].
  852.  
  853.    (This might seem odd, but it occurs to me that a good weave pattern
  854. might also make a good score for bell ringers.  When church bells are
  855. rung, a list of "changes" are used.  For example, if 8 bells are being
  856. used, they will, at first, be rung in order: 12345678.  If the first
  857. change is for bells 5 and 6, the bells will then be rung in the order
  858. 12346578.  If the second change is 1 and 2, the next notes are 21346578.
  859. After a long list of changes, the order the bells are rung in can become
  860. quite complex.
  861.  
  862.    For a group of bell-ringers to change the order of the notes, they
  863. must each either delay their bell's next ring, hasten it, or keep it
  864. the same as the time it takes to ring all the bells once.  The length
  865. of time between each ring of a given bell can only be changed a little
  866. each time, though; with an ink-jet weave pattern, we want the same to
  867. apply to the distance between passes.)
  868.  
  869.    Finally, knowing the number of jets J and their separation S, we can
  870. calculate the starting row of any given pass p as follows:
  871.  
  872.      passesperblock = S
  873.      passblock = floor(p / passesperblock)
  874.      offsetinpassblock = p - passblock * passesperblock
  875.      subblocksperblock = gcd(S, J)
  876.      passespersubblock = S / subblocksperblock
  877.      subpassblock = floor(offsetinpassblock / passespersubblock)
  878.      if subpassblock < ceiling(subblocksperblock/2)
  879.          subblockoffset = 2*subpassblock
  880.      else
  881.          subblockoffset = 2*(subblocksperblock-subpassblock)-1
  882.      startingrow = passblock * S * J + offsetinpassblock * J + subblockoffset
  883.  
  884.    We can simplify this down to the following:
  885.  
  886.      subblocksperblock = gcd(S, J)
  887.      subpassblock = floor((p % S) * subblocksperblock / S)
  888.      if subpassblock * 2 < subblocksperblock
  889.          subblockoffset = 2*subpassblock
  890.      else
  891.          subblockoffset = 2*(subblocksperblock-subpassblock)-1
  892.      startingrow = p * J + subblockoffset
  893.  
  894.    So the row number of jet j of pass p is
  895.  
  896.      subblocksperblock = gcd(S, J)
  897.      
  898.      subblockoffset(p)
  899.          = 2*subpassblock       , if subpassblock * 2 < subblocksperblock
  900.          = 2*(subblocksperblock-subpassblock)-1      , otherwise
  901.            where
  902.            subpassblock = floor((p % S) * subblocksperblock / S)
  903.      
  904.      row(j, p) = p * J + subblockoffset(p) + j * S
  905.  
  906.    Together with the inequality 0 <= j < J, we can use this definition
  907. in reverse to calculate the pass number containing a given row, r.
  908. Working out the inverse definition involves a little guesswork, but one
  909. possible result is as follows.  Given a row, r, which is known to be
  910. the first row of a pass, we can calculate the pass number as follows:
  911.  
  912.      subblocksperblock = gcd(S, J)
  913.      subblockoffset = r % subblocksperblock
  914.      pass = (r - subblockoffset) / J
  915.  
  916.    If G==1, we can determine the pass number with this algorithm:
  917.  
  918.      offset = r % J
  919.      pass = (r - offset) / J
  920.      while (offset % S != 0)
  921.      {
  922.        pass--
  923.        offset += J
  924.      }
  925.      jet = offset / S
  926.  
  927.    Generalising, we come up with this algorithm.  Given r, S and J:
  928.  
  929.      G = gcd(S, J)
  930.      passespersubblock = S/G
  931.      subblockoffset = r % G
  932.      subpassblock = subblockoffset / 2  , if subblockoffset % 2 == 0
  933.                   = G - (subblockoffset+1)/2    , otherwise
  934.      baserow = r - subblockoffset - (subpassblock * passespersubblock * J)
  935.      offset = baserow % J
  936.      pass = (baserow - offset) / J
  937.      while (offset % S != 0)
  938.      {
  939.        offset += J
  940.        pass -= 1
  941.      }
  942.      subblockretreat = floor(pass / passespersubblock) % G
  943.      pass -= subblockretreat * passespersubblock
  944.      pass += subpassblock * passespersubblock
  945.      jet = (r - subblockoffset - pass * J) / S
  946.  
  947.    Let's look at some examples of imperfect but correct weave patterns:
  948.  
  949. S=6,  J=4,  GCD=2,
  950. passesperblock=S=6,
  951. passespersubblock=S/G=6/2=3:
  952.  
  953.      0 *-----*-----*-----*
  954.      1     *-----*-----*-----*
  955.      2         *-----*-----*-----*
  956.      3              *-----*-----*-----*
  957.      4                  *-----*-----*-----*
  958.      5                      *-----*-----*-----*
  959.      6                         *-----*-----*-----*
  960.      7                             *-----*-----*-----*
  961.      8                                 *-----*-----*-----*
  962.      9                                      *-----*-----*-----*
  963.      10                                         *-----*-----*-----*
  964.      11                                             *-----*-----*-----*
  965.      12                                                *-----*-----*-----*
  966.      13                                                    *-----*-----*-----*
  967.      14                                                        *-----*-----*-----*
  968.      15                                                             *-----*-----*----
  969.      16                                                                 *-----*-----*
  970.      17                                                                     *-----*--
  971.      18                                                                        *-----
  972.      19                                                                            *-
  973.  
  974. S=8,  J=6,  G=2,
  975. passesperblock=S=8,
  976. passespersubblock=S/G=8/2=4:
  977.  
  978.      0 *-------*-------*-------*-------*-------*
  979.      1       *-------*-------*-------*-------*-------*
  980.      2             *-------*-------*-------*-------*-------*
  981.      3                   *-------*-------*-------*-------*-------*
  982.      4                          *-------*-------*-------*-------*-------*
  983.      5                                *-------*-------*-------*-------*-------*
  984.      6                                      *-------*-------*-------*-------*-------*
  985.      7                                            *-------*-------*-------*-------*--
  986.      8                                                 *-------*-------*-------*-----
  987.      9                                                       *-------*-------*-------
  988.      10                                                            *-------*-------*-
  989.      11                                                                  *-------*---
  990.      12                                                                         *----
  991.  
  992. S=6,  J=12,  G=6,
  993. passesperblock=S=6,
  994. passespersubblock=S/G=6/6=1:
  995.  
  996.      0 *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*
  997.      1               *-----*-----*-----*-----*-----*-----*-----*-----*-----*-----*---
  998.      2                             *-----*-----*-----*-----*-----*-----*-----*-----*-
  999.      3                                          *-----*-----*-----*-----*-----*-----*
  1000.      4                                                    *-----*-----*-----*-----*--
  1001.      5                                                              *-----*-----*----
  1002.      6                                                                         *-----
  1003.  
  1004.    We have now solved the basic weaving problem.  There are two further
  1005. refinements we need to consider: oversampling, and filling in the
  1006. missing rows at the start of the weave.
  1007.  
  1008.